package cn.kgc.controller;

import cn.kgc.commons.annoation.AutoDict;
import cn.kgc.commons.util.UserContextHolder;
import cn.kgc.constant.PmsAttrTypeName;
import cn.kgc.constant.PmsSearchTypeName;
import cn.kgc.entity.PmsAttr;
import cn.kgc.dto.PmsAttrAddDTO;
import cn.kgc.dto.PmsAttrUpdateDTO;
import cn.kgc.entity.PmsCategory;
import cn.kgc.service.PmsAttrService;
import cn.kgc.service.PmsCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * (PmsAttr)表控制层
 *
 * @author makejava
 * @since 2022-11-29 15:49:22
 */
@RestController
@RequestMapping("pms/attr")
@Api(tags = "商品属性表")
@Validated
public class PmsAttrController {
    /**
     * 服务对象
     */
    @Resource
    private PmsAttrService pmsAttrService;

    @Resource
    private PmsCategoryService pmsCategoryService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "attrType", value = "属性类型:0-销售属性,1-基本属性,2-全部属性", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "searchType", value = "是否需要检索:0-不需要,1-需要,2-全部", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "attrName", value = "属性名", paramType = "query", dataType = "String")
    })
    @CrossOrigin
    @AutoDict
    public IPage<PmsAttr> selectAll(@RequestParam(defaultValue = "1") Integer current,
                                    @RequestParam(defaultValue = "5") Integer pageSize,
                                    @RequestParam(defaultValue = "2") Integer attrType,
                                    @RequestParam(defaultValue = "2") Integer searchType,
                                    @RequestParam(defaultValue = "") String attrName) {
        //封装查询条件
        LambdaQueryWrapper<PmsAttr> lambdaQueryWrapper=new LambdaQueryWrapper<>();
       //如果属性类型不是全部
        lambdaQueryWrapper.eq(attrType!= PmsAttrTypeName.ALL.getAttrType(),PmsAttr::getAttrType,attrType);

       //如果检索类型不是全部
        lambdaQueryWrapper.eq(searchType!= PmsSearchTypeName.ALL.getSearchType(),PmsAttr::getSearchType,searchType);

        lambdaQueryWrapper.like(StringUtils.isNotBlank(attrName),PmsAttr::getAttrName,attrName);
        IPage<PmsAttr> page = new Page(current, pageSize);
        page= pmsAttrService.page(page,lambdaQueryWrapper);
        List<PmsAttr> pmsAttrList=page.getRecords();//获取分页数据
        for (int i = 0; i < pmsAttrList.size(); i++) {
            PmsAttr pmsAttr=pmsAttrList.get(i);
            Long categoryId=pmsAttr.getCatelogId();
            PmsCategory pmsCategory=pmsCategoryService.getById(categoryId);
            if (pmsCategory!=null){
                pmsAttr.setCatelogName(pmsCategory.getName());
            }
        }

        System.out.println(UserContextHolder.getInstance().getUserName());
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public PmsAttr selectOne(@PathVariable Serializable id) {
        //todo 自己转换下常量
        return this.pmsAttrService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param pmsAttrDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody PmsAttrAddDTO pmsAttrDTO) {
        //todo 验证新增的属性值是否正确
        return pmsAttrService.saveAttrAndRelation(pmsAttrDTO);
    }

    /**
     * 修改数据
     *
     * @param pmsAttrDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean update(@RequestBody PmsAttrUpdateDTO pmsAttrDTO) {
        return pmsAttrService.updateAttrAndRelation(pmsAttrDTO);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除属性", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.pmsAttrService.deleteAttrAndRelation(id);
    }
}
